<template>

  <div style="height: 100vh;background-image:url('src/img/WechatIMG75.jpg');background-size: cover;overflow: hidden ">
    <el-container>
      <!--  头部信息：log -->
      <el-header style="padding: 30px">
<!--        栅格之间间隔50-->
        <el-row :gutter="50">
          <el-col :span="19">
            <span style="font-size: 30px;font-weight: bolder;color:#fff">网上图书购物系统</span>
          </el-col>
        </el-row>
      </el-header>


      <el-main>

        <el-row :gutter="40">
          <el-col :span="14">
            <el-image src=""></el-image>
          </el-col>
          <el-col :span="8">
            <el-card style="margin-top: 80px">
              <h3 style="text-align: center">用户登录</h3>
              <el-form label-position="top">
                <el-form-item label="用户名">
                  <el-input placeholder="请输入用户名" v-model="user.username"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                  <el-input type="password" placeholder="请输入密码" v-model="user.password"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" style="width: 100%" @click="userLogin">登录</el-button>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" style="width: 100%" @click="userReg()">注册</el-button>
                </el-form-item>
              </el-form>
            </el-card>

          </el-col>
        </el-row>
      </el-main>
    </el-container>
  </div>

</template>

<script setup>
import {ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import router from "@/router";
import { useUserStore } from "@/stores";

// 定义user数据，用来存储登录信息
const user = ref({username:"",password:""});
const userItem = ref({id:"",username:"",status:"",money:"",nickname:"",address:"",phone:"",avartar:""});
// 登录方法
const userLogin = () =>{
  console.log(user.value)
  axios.post("/api/UserLoginServlet",user.value)
      .then((response) =>{
        if (response.data.code === 1){
          ElMessage.success("登录成功");
          userItem.value = response.data.data;
          // 将用户信息存储到localStorage中
          console.log(response);
          useUserStore().setUserInfo(response.data.data)
          // 跳转到首页
          router.push("/");
        }else {
          ElMessage.error("登录失败");
        }
      })
}

const userReg =() =>{
  router.push("/register");
}

</script>

<style scoped>

</style>